<!doctype html>
<html>

  <head>
    <meta charset="utf-8">
    <title>待办详情</title>
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <link href="../../../css/mui.min.css" rel="stylesheet" />
    <link href="../css/common.css" rel="stylesheet" />
    <link href="../css/iconfont.css" rel="stylesheet" />
    <style>
      .mui-btn, button, input[type=button], input[type=reset], input[type=submit] {
        padding: 6px 10px;
      }
      .mui-popover {
      				height: 130px;
              width: 200px;
      			}
    </style>
  </head>

  <body>
    <div class="container" id="container">
      <header class="mui-bar mui-bar-nav">
        <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
        <h1 class="mui-title">设备档案详情</h1>
        <a class="mui-pull-right iconfont icon-bianji mui-right-icon" href="#topPopover"> 操作</a>
      </header>
      <!--右上角弹出菜单-->
      <div id="topPopover" class="mui-popover">
        <div class="mui-popover-arrow"></div>
        <div class="mui-scroll-wrapper">
          <div class="mui-scroll">
            <ul class="mui-table-view">
              <!-- <li class="mui-table-view-cell" @click="offline()">
                <a href="javascript:;">离线存储</a>
              </li> -->
              <li class="mui-table-view-cell" @click="offline()"><a href="#">重新离线</a>
              </li>
              <li class="mui-table-view-cell" @click="submit()"><a href="#">数据提交</a>
              </li>
              <li class="mui-table-view-cell" @click="edit()"><a href="javascript:void(0);">编辑</a>
              </li>
            </ul>
          </div>
        </div>

      </div>
      <div class="mui-content bg-white" style="padding-bottom: 50px;">
        <div class="content-Title">
          <span class="titleB">功能位置编码</span>
        </div>
        <div class="content">
          <div class="item flex">
            <div class="text-grey">功能位置编码:</div>
            <div class="text-black">{{ detail.mdmCode }}</div>
          </div>
          <div class="item flex" v-if="detail.parentName">
            <div class="text-grey">上级设备:</div>
            <div class="text-black">{{ detail.parentName }}</div>
          </div>
        </div>
        <div class="content-Title">
          <span class="titleB">区域信息</span>
        </div>
        <div class="content">
          <div class="item flex">
            <div class="text-grey">所属区域:</div>
            <div class="text-black">{{ detail.areaNames }}</div>
          </div>
          <div class="item flex">
            <div class="text-grey">所属场站:</div>
            <div class="text-black">{{ detail.stationNames }}</div>
          </div>
          <div class="item flex">
            <div class="text-grey">所属项目:</div>
            <div class="text-black">{{ detail.projectNames }}</div>
          </div>
          <div class="item flex">
            <div class="text-grey">项目公司:</div>
            <div class="text-black">{{ detail.projectCompanyName }}</div>
          </div>
        </div>
        <div class="content-Title">
          <span class="titleB">设备信息</span>
        </div>
        <div class="content">
          <div class="item flex">
            <div class="text-grey">设备名称:</div>
            <div class="text-black">{{ detail.deviceName }}</div>
          </div>
          <div class="item flex">
            <div class="text-grey">设备型号:</div>
            <div class="text-black">{{ detail.sn }}</div>
          </div>
        </div>
        <div class="content-Title">
          <span class="titleB">属性信息</span>
        </div>
        <div class="content">
          <!-- {{ detail.model.propDatas}} -->
          <div class="item flex" key="domain.id" v-for="domain in detail.model.propDatas">
            <div class="text-grey">{{domain.mdName}}:</div>
            <div class="text-black"><span class="rowContent" v-if="domain.mdPropType == 0">{{ domain.mdDictValueRemarks || (domain.value?domain.value:'无') }}</span></div>
            <div>
              <span class="form-subtext" v-if="domain.mdUnit">{{domain.mdUnit}}</span>
            </div>
            <div v-if="domain.mdPropType == 10" style="float: left;margin-right: 10px;">
              <span v-if="domain.attachmentPath">{{domain.attachmentName}}</span>
              <span v-else class="text-grey">未上传</span>
              <a href="javascript:void(0);" v-if="domain.attachmentPath" @click="open(domain)">预览</a></div>
          </div>


          <!-- <div class="item flex">
            <div class="text-grey">制度标准规范:</div>
            <div class="text-black flex-sub">制度标准规范.doc 5M</div>
            <div class=""><a href="" class="padding-right-xs">下载</a><a href="">预览</a></div>
          </div> -->
        </div>
      </div>
      <div class="footer fixed shadow" style="padding:10px 10px">
        <div class="flex justify-center">
          <button type="button" class="mui-btn mui-btn-primary" @click="view()">子设备</button>
          <!-- <button type="button" class="mui-btn mui-btn-primary">设备台账</button>
          <button type="button" class="mui-btn mui-btn-primary">巡检记录</button> -->
          <!-- <button type="button" class="mui-btn mui-btn-primary" @click="record()">操作记录</button> -->
          <!-- <button type="button" class="mui-btn mui-btn-primary">编辑</button> -->
        </div>
      </div>
      <div class="popUp" id="popUp" style="display: none;">
        <form action="" class="popUpContent">
          <div class="item flex">
            <div class="label">审批：</div>
            <div class="flex">
              <div class="mui-input-row mui-radio mui-left">
                <label>通过</label>
                <input name="radio1" type="radio">
              </div>
              <div class="mui-input-row mui-radio mui-left">
                <label>未通过</label>
                <input name="radio1" type="radio" checked>
              </div>
            </div>
          </div>
          <div class="item flex">
            <div class="label">意见：</div>
            <div class="flex-sub ">
              <textarea id="textarea" rows="5" placeholder="多行文本框"></textarea>
            </div>
          </div>
          <div class="item flex">
            <div class="label"></div>
            <div class="flex-sub flex">
              <button type="button" class="mui-btn mui-btn-primary mui-btn-outlined flex-sub">取消</button>
              <button type="button" class="mui-btn mui-btn-primary flex-sub">确认</button>
            </div>
          </div>
        </form>
      </div>
    </div>

  </body>
  <script src="../../../js/mui.min.js"></script>
  <script src="../../../js/vue.js"></script>
  <script src="../../../js/jquery-1.7.2.min.js"></script>
  <script src="../js/mkeyplus.base.js"></script>
  <script src="../js/sqlite.js"></script>
  <script type="text/javascript">
    mui.init()
    var mask = null;
    var detailVm = null;
    var cv = null; // 当前webview
    var pageId = ""; //获取详情id
    var databaseName = "offlineFiles"; //定义表的名字
    var bdName = "files"; //数据库的名字
    var dictName = "offlineDicts"; //字典表的名字
    mui.plusReady(function() {
      cv = plus.webview.currentWebview();
      pageId = cv.pageId || '';
      console.log(pageId)
      if (!validBdOpen(bdName)) {
        createDb(bdName)
      }
      detailVm.getDetail(pageId)
    })
    detailVm = new Vue({
      el: "#container",
      data: {
        detail: {
          model:{
            propDatas: [],
          },
          
        },
        mainId: '', //主设备id
        isEdit: 0 //是否修改此数据
      },
      created: function() {},
      methods: {
        offline() {
          var that = this;
          mui(".mui-popover").popover("toggle");
          batchOffline([that.detail],[pageId])
        },
        view: function() {
          mui.openWindow({
            url: '../help/fileChildren.html',
            id: '../help/fileChildren.html',
            createNew: true, //是否重复创建同样id的webview，默认为false:不重复创建，直接显示
            extras: {
              pageId: pageId
            }
          })
        },
        record: function() {
          mui.openWindow({
            url: '../file/record.html',
            id: '../file/record.html',
            extras: {
              pageId: pageId
            }
          })
        },
        edit: function() {
          mui(".mui-popover").popover("toggle");
          mui.openWindow({
            url: '../help/filesEdit.html',
            id: '../help/filesEdit.html',
            createNew: true, //是否重复创建同样id的webview，默认为false:不重复创建，直接显示
            extras: {
              pageId: pageId
            }
          })
        },
        getDetail: function(pageId) {
          var that = this;
          selectListSQL(bdName, "select * from " + databaseName + " WHERE id ='" + pageId + "'",
            function(data) {
              if (data.length > 0) {
                that.mainId = data[0].mainId;
                that.isEdit = data[0].isEdit;
                var data = JSON.parse(data[0].detail)
                let {childDocuments,model} = data;
                let { childModels, propDatas } = model;
                if (propDatas) {
                  var enumNames = [];
                  propDatas.forEach(propData => {
                    if (propData.mdJavaType == "Enum" && propData.mdDictName) {
                      enumNames.push(propData.mdDictName);
                    }
                  });
                  if (enumNames.length) {
                    //查询字典表
                    selectListSQL(bdName, "select * from " + dictName + "", function(dict) {

                      that.dictDatas = JSON.parse(dict[0].dictItem);
                      data.model.propDatas = propDatas.map(propData => {
                        if (propData.mdJavaType == "Enum" && propData.mdDictName) {
                          var dictList = that.dictDatas[propData.mdDictName];
                          if (dictList && propData.value) {
                            for (var i of dictList) {
                              // console.log('mdDictValueRemarks', i, propData.value)
                              if (i.value == propData.value) {
                                propData.mdDictValueRemarks = i.label;
                                break;
                              }
                            }
                          }
                        }
                        return propData;
                      });
                      that.detail = data;
                    })
                  } else {
                    data.model.propDatas = propDatas.map((propData, i) => {
                      // console.log('propData', self.dictDatas, propData.mdDictName)
                      return {
                        ...propData,
                        dictDatas: propData.mdJavaType == "Enum" && propData.mdDictName ?
                          self.dictDatas[propData.mdDictName] : []
                      };
                    });
                  }
                }
                that.detail = data;
              }
            })
          plus.nativeUI.closeWaiting();
        },
        submit() { //编辑提交
          mui(".mui-popover").popover("toggle");
          if (plus.networkinfo.getCurrentType() == plus.networkinfo.CONNECTION_NONE) {
            mui.toast('请在有网的状态下做提交！')
            return
          }
          var that = this;
          //查询所有修改的主设备,做提交
          selectListSQL(bdName, "select * from offlineFiles WHERE mainId ='" + that.mainId +
            "' and isEdit = 1",
            function(data) {
              var length=data.length
              if (data.length > 0) {
                data.forEach(function(item, index) {
                  plus.nativeUI.showWaiting('提交中...');
                  mui.baseApp.httpRequst("/api/metadataDocument", JSON.parse(item.detail), function(data) {

                    var Sql = "UPDATE offlineFiles SET isEdit = 0 WHERE id ='" + item.id + "'"; //修改主设备
                    selectListSQL(bdName, Sql, function(data) {
                      console.log('修改状态成功')
                    });
                    var page = plus.webview.getWebviewById("../help/files.html");
                    mui.fire(page, 'update'); 
                    plus.nativeUI.closeWaiting();
                    if(index===length-1){
                       plus.webview.currentWebview().close();
                    }
                  }, function() {
                    plus.nativeUI.closeWaiting();
                    console.log("提交失败");
                  }, 'put')
                })
              } else {
                mui.toast('此设备您还未做修改！')
              }
            });
          return;
          var that = this;
          var form = that.detail;

        },
        open: function(item) {
         var FileSystemURL = "_downloads/"+ item.id + "/" + item.attachmentName;
          plus.io.resolveLocalFileSystemURL(FileSystemURL, function(){
              var fileUrl = FileSystemURL;
              plus.runtime.openFile(fileUrl, {}, function(e) {
                alert('打开失败');
              });
          }, function(){
            mui.toast('找不到此文件')
          })
        }
      },

    })
    window.addEventListener("update", function(e) {
      console.log('更新数据');
      detailVm.getDetail(pageId);
      var page = plus.webview.getWebviewById("../help/files.html");
      mui.fire(page, 'update');
    });
    mask = mui.createMask(function() {
      $('#popUp').hide();
    });
    // mask.show()
  </script>
</html>
